package com.lihaozhe.orderservice.controller;

import com.lihaozhe.orderservice.dto.OrderDTO;
import com.lihaozhe.orderservice.service.OrderService;
import com.lihaozhe.userservice.dto.UserDTO;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 订单服务控制器 - Feign版本
 * 演示Feign客户端的各种使用场景
 *
 * @author 李昊哲
 * @version 1.0.0
 */
@RestController
@RequestMapping("/api/orders")
public class OrderController {

  private final OrderService orderService;

  public OrderController(OrderService orderService) {
    this.orderService = orderService;
  }

  /**
   * 创建订单 - Feign版本
   * POST /api/orders
   */
  @PostMapping
  public Map<String, Object> createOrder(@RequestBody Map<String, Object> orderRequest) {
    return orderService.createOrder(orderRequest);
  }

  /**
   * 根据订单ID获取订单详情
   * GET /api/orders/{orderId}
   */
  @GetMapping("/{orderId}")
  public OrderDTO getOrder(@PathVariable("orderId") Long orderId) {
    return orderService.getOrder(orderId);
  }

  /**
   * 获取用户的所有订单
   * GET /api/orders/user/{userId}
   */
  @GetMapping("/user/{userId}")
  public List<OrderDTO> getUserOrders(@PathVariable("userId") Long userId) {
    return orderService.getUserOrders(userId);
  }

  /**
   * 获取所有用户信息 - 演示Feign调用
   * GET /api/orders/users/all
   */
  @GetMapping("/users/all")
  public List<UserDTO> getAllUsers() {
    return orderService.getAllUsers();
  }

  /**
   * 根据分类获取商品 - 演示带参数的Feign调用
   * GET /api/orders/products/category/{category}
   */
  @GetMapping("/products/category/{category}")
  public Map<String, Object> getProductsByCategory(@PathVariable("category") String category) {
    return orderService.getProductsByCategory(category);
  }

  /**
   * Feign负载均衡测试
   * GET /api/orders/feign-loadbalance-test?calls=20
   */
  @GetMapping("/feign-loadbalance-test")
  public Map<String, Object> feignLoadBalanceTest(@RequestParam(name = "calls",defaultValue = "20") int calls) {
    return orderService.testFeignLoadBalancing(calls);
  }

  /**
   * 健康检查端点
   * GET /api/orders/health
   */
  @GetMapping("/health")
  public Map<String, Object> health() {
    return Map.of(
        "status", "UP",
        "service", "order-service",
        "timestamp", System.currentTimeMillis(),
        "totalOrders", orderService.getUserOrders(1L).size(), // 示例调用
        "feignEnabled", true
    );
  }

  /**
   * Feign功能演示端点
   * GET /api/orders/feign-demo
   */
  @GetMapping("/feign-demo")
  public Map<String, Object> feignDemo() {
    try {
      // 演示各种Feign调用
      List<UserDTO> users = orderService.getAllUsers();
      Map<String, Object> phoneProducts = orderService.getProductsByCategory("手机");

      return Map.of(
          "usersCount", users != null ? users.size() : 0,
          "phoneProductsCount", phoneProducts != null && phoneProducts.containsKey("products") ?
              ((List) phoneProducts.get("products")).size() : 0,
          "message", "Feign客户端演示成功",
          "timestamp", System.currentTimeMillis()
      );
    } catch (Exception e) {
      return Map.of(
          "error", e.getMessage(),
          "message", "Feign客户端演示失败"
      );
    }
  }
}
